package linkedlist;

/**
 * Created with IntelliJ IDEA.
 * Description: 链表相交
 * User: Li_yizYa
 * Date: 2025—03—25
 * Time: 22:24
 */
public class Solution5 {
    public static void main(String[] args) {

    }
    private static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        ListNode curA = headA;
        ListNode curB = headB;
        while (curA != curB) {
            curA = curA == null ? headB : curA.next;
            curB = curB == null ? headA : curB.next;
        }
        return curA;
    }
    private static ListNode getIntersectionNode1(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) {
            return null;
        }
        int lenA = 0;
        int lenB = 0;
        int len = 0;
        ListNode curA = headA;
        ListNode curB = headB;

        while (curA != null) {
            curA = curA.next;
            lenA++;
        }

        while (curB != null) {
            curB = curB.next;
            lenB++;
        }
        //此处curA记录长链表，curB记录短链表
        if (lenA > lenB) {
            curA = headA;
            curB = headB;
            len = lenA - lenB;
        } else {
            curA = headB;
            curB = headA;
            len = lenB - lenA;
        }
        //长链表先走len
        while (len > 0) {
            curA = curA.next;
            len--;
        }

        while (curA != null && curB != null) {
            if (curA == curB) {
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}
